<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FrontendExport;
use App\Http\Controllers\Controller;
use App\Jobs\DownloadsJob;
use App\Models\Download;
use App\Models\FrontendUser;
use Carbon\Carbon;
use Vinlon\Laravel\LayAdmin\Exceptions\AdminException;

class UserController extends Controller
{
    /** 查询列表 */
    public function index()
    {
        $query = FrontendUser::query()
            ->with(['auth', 'agent'])
            ->when(request()->keyword, function ($q) {
                $likeVal = '%' . request()->keyword . '%';

                return $q->where('nickname', 'like', $likeVal)->orWhere('mobile', 'like', $likeVal);
            })
            ->when(request()->agent_id, function ($q) {
                return $q->where('agent_id', request()->agent_id);
            })
            ->when(request()->real_name, function ($q) {
                if (1 == request()->real_name) {
                    return $q->has('auth');
                }

                return $q->doesntHave('auth');
            })
            ->when(request()->start_date, function ($q) {
                $startTime = Carbon::parse(request()->start_date)->startOfDay();

                return $q->where('created_at', '>=', $startTime);
            })
            ->when(request()->end_date, function ($q) {
                $endTime = Carbon::parse(request()->end_date)->endOfDay();

                return $q->where('created_at', '<=', $endTime);
            })
            ->orderByDesc('id');

        return paginate_result($query);
    }

    public function export()
    {
        $user = auth()->user();
        $file = '用户导出' . date('YmdHis') . uniqid() . '.xls';

        $count = $this->getUserQuery(request()->agent_id)->count();
        if ($count > 50000) {
            throw new AdminException('导出用户数量大于5万条，请增加筛选条件分批导出');
        }
        if (0 == $count) {
            throw new AdminException('未查到用户记录');
        }
        $download = Download::create([
            'agent_id' => 0,
            'user_id' => $user->id,
            'name' => $file,
            'url' => 'storage/' . $file,
            'status' => 0,
            'model' => FrontendExport::class,
            'request' => json_encode(request()->all()),
        ]);

        dispatch(new DownloadsJob($download));
    }

    /** 删除用户的实名信息 */
    public function deleteUserAuth($id)
    {
        $user = FrontendUser::query()->find($id);
        $user->auth()->delete();
    }

    private function getUserQuery($agentId)
    {
        return FrontendUser::query()
            ->when(request()->keyword, function ($q) {
                $likeVal = '%' . request()->keyword . '%';

                return $q->where('nickname', 'like', $likeVal)->orWhere('mobile', 'like', $likeVal);
            })
            ->when(request()->start_date, function ($q) {
                $startTime = Carbon::parse(request()->start_date)->startOfDay();

                return $q->where('created_at', '>=', $startTime);
            })
            ->when(request()->end_date, function ($q) {
                $endTime = Carbon::parse(request()->end_date)->endOfDay();

                return $q->where('created_at', '<=', $endTime);
            })->when(!empty($agentId), function ($query) use ($agentId) {
                return $query->where('agent_id', $agentId);
            })->when(!empty(request()->real_name), function ($q) {
                if (1 == request()->real_name) {
                    return $q->has('auth');
                }

                return $q->doesntHave('auth');
            });
    }
}
